import time
from modelscope.pipelines import pipeline
from modelscope.utils.constant import Tasks
from models.ai.config.settings import logger

model_path="/home/app/data/modelscope/model/iic/speech_paraformer-large-vad-punc_asr_nat-zh-cn-16k-common-vocab8404-pytorch"
vad_model="/home/app/data/modelscope/model/iic/speech_fsmn_vad_zh-cn-16k-common-pytorch"
punc_model="/home/app/data/modelscope/model/iic/punc_ct-transformer_zh-cn-common-vocab272727-pytorch"

class SpeechToTextService:
    def convert(self, audio_file):
        start_time = time.time()
        inference_pipeline = pipeline(
            task=Tasks.auto_speech_recognition,
            model=model_path,
            vad_model=vad_model,
            punc_model=punc_model)

        rec_result = inference_pipeline(audio_file)
        end_time = time.time()
        logger.info(f"耗时:{end_time - start_time}, audio_file={audio_file}")
        return rec_result[0]['text']
